//index.js
//获取应用实例
import Config from '../../etc/config'
var app = getApp()
Page({
  data: {
    checkTags: []
  },
  onLoad(e) {
    wx.hideShareMenu()
    this.getAllLabels(e.shopId)
    let checkTags = JSON.parse(e.checkTags)
    this.setData({
      checkTags: checkTags
    })
  },
  getAllLabels(shopId) {
    wx.showLoading({
      title: '加载中'
    });
    let params = {
      shopId: shopId
    }
    let token = wx.getStorageSync("token");
    Config._post('/merServicesType/getAllLabel', token, params).then(res => {
      wx.hideLoading()
      if (res.data) {
        let labelList = res.data
        this.setLabelChecked(this.data.checkTags, labelList)
        this.setData({
          label: labelList
        })
      }
    })
  },
  setLabelChecked(sourceList, desList) {
    for (let i = 0; i < sourceList.length; i++) {
      for (let j = 0; j < desList.length; j++) {
        if (sourceList[i].id == desList[j].labelId) {
          desList[j].checked = true
        }
      }
    }
  },
  tagClick(e) {
    let idx = e.currentTarget.dataset.index
    if (this.data.label[idx].checked == undefined) {
      this.data.label[idx].checked = false
    }
    let index = 'label[' + idx + '].checked',
      checked = !this.data.label[idx].checked
    this.setData({
      [index]: checked
    })
  },
  submit() {
    var checkList = []
    for (let i = 0; i < this.data.label.length; i++) {
      if (this.data.label[i].checked) {
        let option = {
          name: this.data.label[i].labelName,
          id: this.data.label[i].labelId
        }
        checkList.push(option)
      }
    }
    var pages = getCurrentPages(); // 获取页面栈
    var prevPage = pages[pages.length - 2]; // 上一个页面
    prevPage.setData({
      checkTags: checkList
    })
    wx.navigateBack()
  }
})